//移入移出
require(["../../static/conf/config.js"], function () {
    require(["jquery"], function ($) {
        let $city = $(".current-city")
        let $cityList = $(".current-city-list")
        let $message = $(".header-message")
        let $messageList = $(".message-list-header")
        let $applist = $("#app-list")
        let $APP = $("#APP")
        let $weixin = $(".footer-weixin")
        let $weixinErweima = $(".footer-weixin-erweima")
        let $footApp = $(".footer-app")
        let $appErweima = $(".footer-app-erweima")
        let $headerUser = $(".header-user")
        let $HeaderUser = $("#header-user")
        $headerUser.mouseenter(function() {
            $HeaderUser.show("slow");
        })
        $HeaderUser.mouseleave(function(){
            $HeaderUser.hide();
        })
        $weixin.mouseenter(function() {
            $weixinErweima.show("slow");
        })
        $weixin.mouseleave(function(){
            $weixinErweima.hide();
        })
        $footApp.mouseenter(function() {
            $appErweima.show("slow");
        })
        $footApp.mouseleave(function(){
            $appErweima.hide();
        })
        $APP.mouseenter(function() {
            $applist.show("slow");
        })
        $applist.mouseleave(function(){
            $applist.hide();
        })
        $city.mouseenter(function() {
            $cityList.show("slow");
        })
        $cityList.mouseleave(function(){
            $cityList.hide();
        })
        $message.mouseenter(function() {
            $messageList.show("slow");
        })
        $messageList.mouseleave(function(){
            $messageList.hide();
        })
    })
})

//登录状态的检测和退出登录
require(["../../static/conf/config.js"], function () {
    require(["jquery"], function ($) {
        $(document).ready(function () {

            let obj = localStorage.getItem('logined');
            if (obj !== null) {
                $(".logined").show()
                $(".not-login").hide()
            }
            if (obj == null) {
                $(".logined").hide()
                $(".not-login").show()
            }

            $(".quit").click(function () {
                localStorage.removeItem("logined")
                $(".logined").hide()
                $(".not-login").show()
            })

        })

    })
})

//模板引擎
let module = JSON.parse(localStorage.getItem("cart"))
    console.log(module[0])
    let product = document.getElementById("view");

    function template(id, module) {
    let template = document.getElementById(id).innerHTML;

    template = 'print(`' + template + '`)';
    template = template.replace(/<%=(.+?)%>/g, '`) \n print( $1 ) \n print(`');
    template = template.replace(/<%(.+?)%>/g, '`) \n $1 \n print(`');

    var codestr = `
        (function(data) {
            var htmlstr = "";
            function print(val){
                htmlstr += val;
            }
            ${template}
            return htmlstr;
        })
    `;
    let fun = eval(codestr);
    return fun(module);
    }   
    
    product.innerHTML = template("modules",module)

    require(["../../static/conf/config.js"], function () {
            require(["jquery"], function ($) {
                
                $(".plus").click(function(){
                    let i = $(this).siblings(".value").val();
                    i++;
                    console.log(i)
                    $(this).siblings(".value").attr({
                        value : i
                    })
                    $(this).siblings(".minus").removeAttr("disabled")
                    let m = (parseInt($(this).siblings(".value").val()) * parseInt($(this).parent().parent().siblings(".cart-unit-Price").text())).toFixed(2)
                    $(this).parent().parent().siblings(".money").text(m) ;
                })
                $(".minus").click(function(){
                    let i = $(this).siblings(".value").val();
                    i--;
                    $(this).siblings(".value").attr({
                        value : i
                    })
                    if($(this).siblings(".value").val()=="1"){
                        $(this).attr({
                        disabled : "disabled" ,
                    })
                    }
                    let m = (parseInt($(this).siblings(".value").val()) * parseInt($(this).parent().parent().siblings(".cart-unit-Price").text())).toFixed(2)
                    $(this).parent().parent().siblings(".money").text(m) ;
                })

                    let cart = JSON.parse(localStorage.getItem("cart"))
                    for(var i=0;i<$(".delete i").length;i++){
                        $(".delete")[i].index = i; 
                        $(".delete i").click(function(){
                            delete cart[i]
                            localStorage.setItem(JSON.stringify(cart))
                        })
                    }
                    
                    $(".cart-submit-empty").click(function(){
                        localStorage.clear()
                    })

                console.log(parseInt($(".money").text()))
                
                console.log(typeof($(".value").val()))
                console.log(cart)
                
                $(document).ready(function($){

                
                if( cart !=null){
                    $(".car").css({
                        "display" : "block"
                    })
                    $(".cart-not-pro").css({
                        "display" : "none"
                    })
                }
                if( cart == null){
                    $(".car").css({
                        "display" : "none"
                    })
                    $(".cart-not-pro").css({
                        "display" : "block"
                    })
                }

                })

        })
    })
    